package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Action_Request类
 * 实现动作请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Action_Request implements AxdrType {

    private byte[] dataCode = null;
    private ActionRequestChoice choice = ActionRequestChoice._ERR_NONE_SELECTED;
    private ActionRequestNormal actionRequestNormal = null;
    private ActionRequestList actionRequestList = null;
    private ActionThenGetRequestNormalList actionThenGetRequestNormalList = null;

    public Action_Request() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setActionRequestNormal(ActionRequestNormal actionRequestNormal) {
        this.resetChoices();
        this.choice = ActionRequestChoice.ACTION_REQUEST_NORMAL;
        this.actionRequestNormal = actionRequestNormal;
    }

    public void setActionRequestList(ActionRequestList actionRequestList) {
        this.resetChoices();
        this.choice = ActionRequestChoice.ACTION_REQUEST_LIST;
        this.actionRequestList = actionRequestList;
    }

    public void setActionThenGetRequestNormalList(ActionThenGetRequestNormalList actionThenGetRequestNormalList) {
        this.resetChoices();
        this.choice = ActionRequestChoice.ACTION_THEN_GET_REQUEST_NORMAL_LIST;
        this.actionThenGetRequestNormalList = actionThenGetRequestNormalList;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        com.dl698.asn1.axdr.AxdrEnum choosen = new com.dl698.asn1.axdr.AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = ActionRequestChoice.fromValue((int) choosen.getValue());

        if (this.choice == ActionRequestChoice.ACTION_REQUEST_NORMAL) {
            this.actionRequestNormal = new ActionRequestNormal();
            codeLength += this.actionRequestNormal.decode(input);
            return codeLength;
        } else if (this.choice == ActionRequestChoice.ACTION_REQUEST_LIST) {
            this.actionRequestList = new ActionRequestList();
            codeLength += this.actionRequestList.decode(input);
            return codeLength;
        } else if (this.choice == ActionRequestChoice.ACTION_THEN_GET_REQUEST_NORMAL_LIST) {
            this.actionThenGetRequestNormalList = new ActionThenGetRequestNormalList();
            codeLength += this.actionThenGetRequestNormalList.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (java.io.IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == ActionRequestChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == ActionRequestChoice.ACTION_THEN_GET_REQUEST_NORMAL_LIST) {
            if (this.actionThenGetRequestNormalList != null) {
                codeLength += this.actionThenGetRequestNormalList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("actionThenGetRequestNormalList is null");
            }
        } else if (this.choice == ActionRequestChoice.ACTION_REQUEST_LIST) {
            if (this.actionRequestList != null) {
                codeLength += this.actionRequestList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("actionRequestList is null");
            }
        } else if (this.choice == ActionRequestChoice.ACTION_REQUEST_NORMAL) {
            codeLength += this.actionRequestNormal.encode(output);
            com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }

        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding Action_Request", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public ActionRequestChoice getChoiceIndex() {
        return this.choice;
    }

    public ActionRequestNormal getActionRequestNormal() {
        return this.actionRequestNormal;
    }

    public ActionRequestList getActionRequestList() {
        return this.actionRequestList;
    }

    public ActionThenGetRequestNormalList getActionThenGetRequestNormalList() {
        return this.actionThenGetRequestNormalList;
    }

    public void resetChoices() {
        this.choice = ActionRequestChoice._ERR_NONE_SELECTED;
        this.actionRequestNormal = null;
        this.actionRequestList = null;
        this.actionThenGetRequestNormalList = null;
    }

    @Override
    public String toString() {
        if (this.choice == ActionRequestChoice.ACTION_REQUEST_NORMAL) {
            if (this.actionRequestNormal != null) {
                return "choice: {action_request_normal: " + this.actionRequestNormal + "}";
            } else {
                return "choice is action_request_normal but actionRequestNormal is null";
            }
        } else if (this.choice == ActionRequestChoice.ACTION_REQUEST_LIST) {
            if (this.actionRequestList != null) {
                return "choice: {action_request_list: " + this.actionRequestList + "}";
            } else {
                return "choice is action_request_list but actionRequestList is null";
            }
        } else if (this.choice == ActionRequestChoice.ACTION_THEN_GET_REQUEST_NORMAL_LIST) {
            if (this.actionThenGetRequestNormalList != null) {
                return "choice: {action_then_get_request_normal_list: " + this.actionThenGetRequestNormalList + "}";
            } else {
                return "choice is action_then_get_request_normal_list but actionThenGetRequestNormalList is null";
            }
        } else {
            return "unknown";
        }
    }
}